/**
 * Created by caosh on 2018/1/18 0018.
 */
var Store = new Vue({
    el: '#new-store',
    created: function () {
        this.flag = store.get('storeFlag');
        if (this.flag == 'new') {
            this.tip = '新增门店';
        } else if (this.flag == 'edit' || this.flag == 'edit-order') {
            this.tip = '编辑门店';
            this.store = store.get('store');
            // store.remove('store');
        }
    },
    data: {
        tip: '新增门店',
        flag: 'new',
        picker: '',
        regionTemp: '广东省广州市荔湾区',
        businessTemp: '0:00-0:00',
        provinceTemp: '广东省',
        cityTemp: '广州市',
        districtTemp: '荔湾区',
        store: {
            province: '',
            city: '',
            district: '',
            region: '',
            location: '',
            phone: '',
            name: '',
            personName: '',
            business: '',
            isDefault: false
        },
        addressSlots: [
            {
                flex: 1,
                defaultIndex: 1,
                values: Object.keys(address),  //省份数组
                className: 'slot1',
                textAlign: 'center'
            }, {
                divider: true,
                content: '-',
                className: 'slot2'
            }, {
                flex: 1,
                values: [],
                className: 'slot3',
                textAlign: 'center'
            },
            {
                divider: true,
                content: '-',
                className: 'slot4'
            },
            {
                flex: 1,
                values: [],
                className: 'slot5',
                textAlign: 'center'
            }
        ],
        businessSlots: [
            {
                flex: 1,
                defaultIndex: 1,
                values: timePicker,
                className: 'slot1',
                textAlign: 'center'
            }, {
                divider: true,
                content: '-',
                className: 'slot2'
            }, {
                flex: 1,
                values: timePicker,
                className: 'slot3',
                textAlign: 'center'
            }
        ]
    },
    methods: {
        // 返回
        goBack: function () {
            window.history.back(-1);
        },
        // 设置默认下单地址
        setDefault: function () {
            this.store.isDefault = !this.store.isDefault;
        },
        // 地址选择器 值改变事件
        onAddressChange: function(picker, values) {
            if(address[values[0]]){  //这个判断类似于v-if的效果（可以不加，但是vue会报错，很不爽）
                picker.setSlotValues(1,Object.keys(address[values[0]])); // Object.keys()会返回一个数组，当前省的数组
                picker.setSlotValues(2,address[values[0]][values[1]]); // 区/县数据就是一个数组
                this.regionTemp = values[0] + values[1] + values[2];
                this.provinceTemp = values[0];
                this.cityTemp = values[1];
                this.districtTemp = values[2];
            }
        },
        // 选择地址
        selectRegion: function () {
            this.store.region = this.regionTemp;
            this.store.province = this.provinceTemp;
            this.store.city = this.cityTemp;
            this.store.district = this.districtTemp;
            this.clearPicker();
        },
        // 显示地址选择器
        toShowRegion: function () {
            this.picker = 'region';
            this.regionTemp = this.store.region?this.store.region:this.regionTemp;
            this.provinceTemp = this.store.province?this.store.province:this.provinceTemp;
            this.cityTemp = this.store.city?this.store.city:this.cityTemp;
            this.districtTemp = this.store.district?this.store.district:this.districtTemp;
        },
        // 显示时间选择器
        toShowTime: function () {
            this.picker = 'time';
            this.businessTemp = this.store.business?this.store.business:this.businessTemp;
        },
        // 时间选择器改变事件
        onBusinessChange: function (picker, values) {
            this.businessTemp = values[0] + '-' + values[1];
        },
        // 选择时间
        selectBusiness: function () {
            this.store.business = this.businessTemp;
            this.clearPicker();
        },
        // 隐藏地址或时间选择器
        clearPicker: function () {
            this.picker = '';
        },
        // 保存方法
        save: function () {
            var _this = this,
                duration = 1000;

            if (!this.validTel(this.store.phone)) {
                return;
            }

            // 请求后台保存
            /**
             $.post(url, this.store, function(result){
                if (result.success) {
                    _this.$toast({
                        message: '保存成功',
                        iconClass: 'iconfont icon-success iconfont-g',
                        className: 'bsz-toast',
                        duration: duration
                    });
                    // 本地缓存默认门店地址 用于下单时使用
                    if (_this.store.isDefault) {
                        store.set('defaultStore', this.store);
                    }

                    // 保存并使用
                    if (this.flag == 'edit-order') {
                        store.set('currentStore', this.store);
                        store.remove('storeFlag');
                    }

                    var t = setInterval(function () {
                        clearInterval(t);
                        _this.goBack();
                    }, duration);

                }
             }, 'json');
             */
            var result = {success: true};
            if (result.success) {
                _this.$toast({
                    message: '保存成功',
                    iconClass: 'iconfont icon-success iconfont-g',
                    className: 'bsz-toast',
                    duration: duration
                });
                // 本地缓存默认门店地址 用于下单时使用
                if (_this.store.isDefault) {
                    store.set('defaultStore', this.store);
                }

                // 保存并使用
                if (this.flag == 'edit-order') {
                    store.set('currentStore', this.store);
                    store.remove('storeFlag');
                }

                var t = setInterval(function () {
                    clearInterval(t);
                    _this.goBack();
                }, duration);

            }
        },
        // 验证手机
        validTel: function (tel) {
            if (tel.length != 11) {
                this.$toast({
                    message: '手机号码不是11位吗？',
                    className: 'bsz-toast-gray',
                    duration: 1500
                });

                return false;
            }

            return true;
        }
    },
    mounted: function () {
        this.$nextTick(function() { //vue里面全部加载好了再执行的函数  （类似于setTimeout）
            this.addressSlots[0].defaultIndex = 0;
            this.businessSlots[0].defaultIndex = 0;
        });
    }
});